<?php


namespace App\Tool;


use App\Constants\ErrorCode;
use Hyperf\Utils\Collection;
use Hyperf\Utils\Context;
use phpDocumentor\Reflection\Types\Mixed_;

/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-19 21:29
 */
class Collect extends Collection
{
    public function __construct($items = [])
    {
        parent::__construct($items);

        $this->registerCollectMacros();
    }

    public function empty($callback = null)
    {
        $isEmpty = empty($this->items);
        if ($callback) {
            return $isEmpty ? $callback(clone $this) : $this;
        } else {
            return empty($this->items);
        }
    }

    private function registerCollectMacros()
    {
        $self = $this;
        $this::macro('getUserId1', function ($isError) use ($self) {
            return Context::get('uid') ?: ($isError ? err('token is wrong', ErrorCode::TOEKN_INVALID) : 0);
        });
    }

    public function getUserId($isError = true)
    {
        return Context::get('uid') ?: ($isError ? err('token is wrong', ErrorCode::TOEKN_INVALID) : 0);
    }


}